package com.heima.admin.controller;


import com.heima.admin.entity.AdChannel;
import com.heima.admin.service.AdChannelService;
import com.heima.common.dtos.PageResult;
import com.heima.common.dtos.ResponseResult;
import com.heima.common.util.BeanHelper;
import com.heima.model.admin.dtos.ChannelDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController

//分页查询
@RequestMapping("/api/v1/channel")
public class ChannelController {
    //POST /api/v1/channel/list
    @Autowired
    private AdChannelService channelService;
    @PostMapping("/list")
    public PageResult<AdChannel> findByPage(@RequestBody ChannelDto dto){

        return channelService.findByPage(dto);
    }

    //新增
    @PostMapping("/save")
    public ResponseResult save(@RequestBody ChannelDto dto){
        //转变数据
        AdChannel adChannel = BeanHelper.copyProperties(dto, AdChannel.class);
        //创建时间
        adChannel.setCreatedTime(new Date());
        //保存
        channelService.save(adChannel);
        return ResponseResult.ok();
    }
    //修改
    @PutMapping("/update")
    public ResponseResult update(@RequestBody ChannelDto dto){
        //判断

        //转变数据
        AdChannel adChannel = BeanHelper.copyProperties(dto, AdChannel.class);
        //修改
        channelService.updateById(adChannel);
        return ResponseResult.ok();

    }
    //删除
    @DeleteMapping("/del/{id}")
    public ResponseResult delete(@PathVariable(name = "id")Integer channelId ){
        //判断是否再用
        AdChannel adChannel = channelService.getById(channelId);
        if (adChannel.getStatus()){

        }
        //删除
        channelService.removeById(channelId);
        return ResponseResult.ok();

    }



}
